package com.lulu.landslidedemo04;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhanglulu on 2017/3/28. <br/>
 *
 * <p>注：继承BaseLandSlideAdapter后的getView方法中使用position时，请调用getRealPosition()方法</p>
 * <p>注：在给LandSlideView控件设置Adapter之后调用数据更新方法（setData,add,remove...）</p>
 */

public abstract class BaseLandSlideAdapter<T> {
    protected Context mContext;
    private int mItemCount = 5; //每页需要显示条数 默认为5条
    private int mCurPageIndex = 1; //当前页码
    protected volatile ArrayList<T> mList; //所有的数据
    private int mPageCount;
    private NoScrollListViewAdapter<T> mListViewAdapter;
    private boolean mIsHaveFooter = false; //是否包含Footer

    public BaseLandSlideAdapter(Context context) {
        mContext = context;
        mList = new ArrayList<T>();
    }

    /**
     * 数据刷新，重排
     *
     * @return
     */
    private List<T> refreshData() {
        int size = mList.size();
        if (size == 0) {
            //健壮性判断
            return null;
        }
        mPageCount = size / mItemCount +
                (size % mItemCount == 0 ? 0 : 1);
        //添加FooterView
        if (size % mItemCount == 0 && mIsHaveFooter) {
            mPageCount++;
        }

        if (mCurPageIndex > mPageCount) {
            //解决 当将尾页清空后，并就在尾页时，跳转到新的尾页
            mCurPageIndex = mPageCount;
        }
        List<T> tempList = null;
        if (mCurPageIndex - 1 == 0) {
            tempList = mList.subList(0, mList.size() > mItemCount ? mItemCount : mList.size());
        } else if (mCurPageIndex * mItemCount >= mList.size()) {
            tempList = mList.subList((mCurPageIndex - 1) * mItemCount, mList.size());
        } else {
            tempList = mList.subList((mCurPageIndex - 1) * mItemCount, mCurPageIndex * mItemCount);
        }
        mListViewAdapter.setList(tempList);
        if (mRefreshPage != null) {
            mRefreshPage.refreshPageNavigation(mCurPageIndex + "/" + mPageCount);
        }
        return tempList;
    }

    ///////////////////////////////////////////////////////////////////////////
    // ListView重写的方法
    ///////////////////////////////////////////////////////////////////////////
    /**
     * ListView的getView方法
     * @param position
     * @param convertView
     * @param parent
     * @return
     */
    public abstract View getView(int position, View convertView, ViewGroup parent);

    public abstract T getItem(int position);

    public int getItemViewType(int position) {
        return 0;
    }

    public int getViewTypeCount() {
        return 1;
    }

    interface OnRefreshPage {
        void refreshPageNavigation(String text);
        void refreshPageFooter(int index, int count);
    }
    private OnRefreshPage mRefreshPage;

    public void setRefreshPage(OnRefreshPage refreshPage) {
        mRefreshPage = refreshPage;
    }
    ///////////////////////////////////////////////////////////////////////////
    // 对外开放的方法
    ///////////////////////////////////////////////////////////////////////////

    public int getRealPosition(int position) {
        return (mCurPageIndex - 1) * mItemCount + position;
    }

    public int getCurPageIndex() {
        return mCurPageIndex;
    }

    public int getPageCount() {
        return mPageCount;
    }

    public void setItemCount(int itemCount) {
        mItemCount = itemCount;
    }


    public void setListViewAdapter(NoScrollListViewAdapter<T> listViewAdapter) {
        mListViewAdapter = listViewAdapter;
    }

    public void setCurPageIndex(int curPageIndex) {
        mCurPageIndex = curPageIndex;
    }

    public void setData(List<T> list) {
        if (mList != null) {
            mList.clear();
        }
        mList.addAll(list);
        try {
            refreshData();
        } catch (Exception e) {
            throw new RuntimeException("请先给LandSlideView控件设置Adapter 之后设置数据");
        }
    }

    public void setHaveFooter(boolean haveFooter) {
        mIsHaveFooter = haveFooter;
    }

    public boolean isHaveFooter() {
        return mIsHaveFooter;
    }

    public void add(T t) {
        mList.add(t);
        refreshData();
    }

    public void add(T t, int position) {
        mList.add(position, t);
        refreshData();
    }

    public void remove(int position) {
        mList.remove(position);
        refreshData();
    }

    public List<T> notifyDataSetChanged() {
        List<T> list = refreshData();
        if (mRefreshPage != null && list != null) {
            mRefreshPage.refreshPageFooter(mCurPageIndex, mPageCount);
        }
        return list;
    }

}
